<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicModal
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'unitmanage:unitFloor:edit'"
    @register="registerModal"
    @ok="handleSubmit"
    width="60%"
    destroyOnClose={true}
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="ViewsUnitmanageUnitFloorForm">
  import { ref, unref, computed,defineProps } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { UnitFloor, unitFloorSave, unitFloorForm } from '/@/api/unitmanage/unitFloor';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('unitmanage.unitFloor');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<UnitFloor>({} as UnitFloor);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('楼层管理') : t('楼层管理'),
  }));

  // 接收传递过来的 buildingId
  const props = defineProps<{
    buildingId: Number;
  }>();

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('楼层数'),
      field: 'floorNum',
      component: 'Input',
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('楼层图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'floor_image',
        fileUrl: computed(() => record.value.picPath),
        uploadType: 'image',
        maxNumber:1,
        // onChange: (fileUrl) => {
        //   record.value.picPath = fileUrl.floor_image;
        //   console.log(fileUrl.maintenanceTaskProcess_image);
         
        // }
      },
      colProps: { md: 24, lg: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ loading: true });
    await resetFields();
    const res = await unitFloorForm(data);
    record.value = (res.unitFloor || {}) as UnitFloor;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setModalProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      let data = await validate();
      data={
        ...data,
        buildingId: props.buildingId,
        picPath:(data.dataMap.floor_imageUrl != null) ? (data.dataMap.floor_imageUrl.split('?')[0]) : record.value.picPath, 
      
      }
      setModalProps({ confirmLoading: true });
      const params: any = {
        // isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
        buildingId: props.buildingId,
        floorNum:data.floorNum,
      };
      console.log(data)
      // console.log('submit', params, data, record);
      const res = await unitFloorSave(params, data);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
